﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JustTest.Author
{
    public partial class AuthorTests : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //if (!this.User.IsInRole("Author"))
                //{
                //    this.Response.Redirect("~/Default.aspx");
                //}
            }
        }

        public IQueryable<JustTest.Models.Test> TestsGridView_GetData()
        {
            var db = new JustTestDb();

            return db.Tests.Where(x => x.User.UserName == this.User.Identity.Name);
        }

        public IQueryable<Category> CategoryDropDown_SelectItem()
        {
            var db = new JustTestDb();

            return db.Categoriess;
        }

        public void TestCreateFormView_InsertItem1()
        {
            var item = new JustTest.Models.Test();
            TryUpdateModel(item);
            var db = new JustTestDb();
            item.Category = db.Categoriess.Find(item.Category.Id);
            item.Description = "Test By " + this.User.Identity.Name;
            item.User = db.Users.First(x => x.UserName == this.User.Identity.Name);
            item.ReleaseDate = DateTime.Now;
            item = db.Tests.Add(item);
            db.SaveChanges();

            this.Response.Redirect("~/Author/AuthorEditTest.aspx?id=" + item.Id);
        }

        protected void TestsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CustomEdit")
            {
                Response.Redirect("~/Author/AuthorEditTest.aspx?id=" + e.CommandArgument);
                Response.End();
            }
        }
    }
}